package Form::Field::PhoneNumberField;
use strict;
use warnings;
use base qw(My::Form::Field::Compound);
use Rose::HTML::Form::Field::PopUpMenu;
use Contact::Phone;

sub build_field {
    my ($self) = shift;
    my %params;
    $params{name} = 'phone_country';
    $params{class} = $self->class if ( $self->class );
    my $country_field =
        Rose::HTML::Form::Field::PopUpMenu->new( %params, style => "width:100px" );
    my $countries = Country::Manager->get_countries( 'sort_by' => 'name' );

    # the initial 'null' option
    $country_field->add_options( '' => 'Select one' );
    foreach my $country ( @{$countries} ) {
        if ( defined( $country->calling_prefix ) ) {
            my $country_name = $country->name;

            # strips out additional bits after commas from country name
            $country_name =~ s/\,.*$//;
            $country_name .= " (" . $country->calling_prefix . ")";
            $country_field->add_options( $country->id => $country_name );
        }
    }
    $self->add_fields( phone_country => $country_field,
                       phone_number  => { 'type' => 'text', 'size' => 20, 'label' => 'Phone Number' }, );
} ## end sub build_field

sub coalesce_value {
    my ($self)       = shift;
    my $country_id   = $self->field('phone_country')->internal_value;
    my $phone_number = $self->field('phone_number')->internal_value;
    if ( $country_id && $phone_number ) {
        my $phone_obj =
            Contact::Phone->new( number => $phone_number, country_id => $country_id, type => 'test' );
        $phone_obj->format_number_by_country();
        return $phone_obj->number;
    }
}

sub decompose_value {
    my ( $self, $value ) = @_;

    # go through each country and test prefix to number
    my $countries = Country::Manager->get_countries( 'sort_by' => 'name' );
    my $matching_calling_prefix_regex;
    my $matching_calling_prefix = '';
    my $matching_country_id;
    foreach my $country ( @{$countries} ) {
        my $calling_prefix       = $country->calling_prefix;
        my $calling_prefix_regex = $calling_prefix;

        # the leading '+' character in the calling prefix needs to be handled
        $calling_prefix_regex =~ s/^\+/\\\+/;
        if ( $value =~ /^$calling_prefix_regex\d+$/ ) {
            if ( length($calling_prefix) > length($matching_calling_prefix) ) {
                $matching_calling_prefix = $calling_prefix;
                $matching_country_id     = $country->id;
                $matching_calling_prefix_regex = $calling_prefix_regex;
            }
        }
    }
    my $rewritten_value = $value;
    $rewritten_value =~ s/^$matching_calling_prefix_regex//;
    if ( $matching_calling_prefix && $rewritten_value ) {
        return { phone_country => $matching_country_id, phone_number => $rewritten_value };
    }
} ## end sub decompose_value

sub html {
    my ($self) = shift;
    my $html = $self->field('phone_country')->html . $self->field('phone_number')->html;
    return $html . $self->html_error();
} ## end sub html

sub validate {
    my ($self) = shift;

    # the superclass validator will handle all the builtins, like required etc.
    if ( !$self->SUPER::validate() ) { return; }
    if (    $self->required
         && $self->field('phone_country')->internal_value
         && $self->field('phone_number')->internal_value )
    {
        return 1;
    } elsif ( !$self->required ) {
        return 1;
    }
} ## end sub validate
1;
